package com.example.controller.handle;

import com.alibaba.dubbo.config.annotation.Reference;
import com.example.common.api.user.ArticleService;
import com.example.common.api.user.UserService;
import com.example.common.model.Article;
import com.example.common.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;


/**
 * @Author:mjt
 * @Description:个人主页显示功能
 */

@Controller
public class MyIndexController {

    @Reference(version = "1.0.0")
    private UserService userService;

    @Reference(version = "1.0.0")
    private ArticleService articleService;

    /**
     * 根据登录后的当前用户名获取用户数据
     * @param modelAndView 数据和视图模型
     * @return 用户所有数据
     */
    @GetMapping("/user-index.do")
    public ModelAndView userIndex(ModelAndView modelAndView,HttpSession session,@RequestParam(value = "username",required = true)String username) throws Exception{

        User user=userService.findUserByName(username);

        modelAndView.addObject("user",user);
        modelAndView.addObject("session",session);

        modelAndView.setViewName("user-index");

        return modelAndView;
    }

    @GetMapping("/user/article.do")
    @ResponseBody
    public ModelAndView userArticle(ModelAndView modelAndView,HttpSession session,@RequestParam(value = "username",required = true)String username)throws  Exception{
        Article article=articleService.findArticleByName(username);

        modelAndView.addObject("article",article);
        modelAndView.addObject("session",session);

        modelAndView.setViewName("templates/user-index");

        return  modelAndView;
    }
}
